<?php require_once('Connections/con2.php');

	if( isset($_SESSION["autenticado"]) != true){
		header("Location: /sgs/?index.php&pg=login&res=erro");
	}

	if($_SESSION["perfil"] == "prefeitura"){
		mysql_select_db($database_con2, $con2);
		$query_rs_req = "SELECT * FROM requisicoes where status = 'aberta' order by data desc";
		$rs_req = mysql_query($query_rs_req, $con2) or die(mysql_error());
		$row_rs_req = mysql_fetch_assoc($rs_req);
		$totalRows_rs_req = mysql_num_rows($rs_req);
	}else if($_SESSION["perfil"] == "unidade"){
		mysql_select_db($database_con2, $con2);
		$query_rs_req = "SELECT * FROM requisicoes where status <> 'finalizada' and unidade = '" . $_SESSION["unidade"] . "' order by data desc";
		$rs_req = mysql_query($query_rs_req, $con2) or die(mysql_error());
		$row_rs_req = mysql_fetch_assoc($rs_req);
		$totalRows_rs_req = mysql_num_rows($rs_req);
	}else if($_SESSION["perfil"] == "divisao"){
		mysql_select_db($database_con2, $con2);
		$query_rs_req = "SELECT * FROM requisicoes where status <> 'finalizada' and divisao = '" . $_SESSION["divisao"]. "'order by data desc";
		$rs_req = mysql_query($query_rs_req, $con2) or die(mysql_error());
		$row_rs_req = mysql_fetch_assoc($rs_req);
		$totalRows_rs_req = mysql_num_rows($rs_req);
	}else{
		mysql_select_db($database_con2, $con2);
		$query_rs_req = "SELECT * FROM requisicoes where status <> 'finalizada' order by data desc";
		$rs_req = mysql_query($query_rs_req, $con2) or die(mysql_error());
		$row_rs_req = mysql_fetch_assoc($rs_req);
		$totalRows_rs_req = mysql_num_rows($rs_req);
	}
	
	
?>
    

<table width="860" border="0">
       <tr>
        <td>&nbsp;</td>
        <td width="87">&nbsp;</td>
        <td width="86">&nbsp;</td>
        <td width="180">&nbsp;</td>
      </tr>
	  <tr>
		<td width="113"><div align="center"><strong>Requisicao</strong></div></td>
		<td><strong>Data</strong></td>     
		<td><strong>Unidade</strong></td>   
		<td><strong>Setor</strong></td>      
		<td width="372"><strong>Descricao</strong></td>                  
	  </tr>    
	</table>
    <hr>

<?php do { ?>
  <?php 	
  	/* DATA PASSADA PELA MÁQUINA */
	$sishora = mktime(date("H") - 1, date("i"), date("s"));
	$data = date("Y-m-d");
	$hora = date("H:i:s", $sishora);	
	$dataAtual = $data . " " . $hora;
	
	$time_inicial = strtotime($row_rs_req['data']);
	$time_final = strtotime($dataAtual);
	
	$dif = $time_final - $time_inicial;	// Calcula a diferenca em segundos	
	$dias = (int)floor( $dif / (60 * 60 * 24) );	// Calcula a diferenca em dias
	
	if ($dias >= 3) { ?>
		<a href="?index.php&pg=encaminhar&id=<?php echo $row_rs_req['id']; ?>&res" title="Requisicao a mais de tres dias na Caixa de Entrada"><span style="color:#333;">
		<div id="safe-result" class="atencao">    
		<table width="860" border="0">
		  <tr>
			<td width="61"><?php echo $row_rs_req['id']; ?></td>        
			<td width="89"><?php $data = $row_rs_req['data']; echo date("d/m/Y", strtotime($data));?></td>                
			<td width="84"><?php echo $row_rs_req['unidade']; ?></td>        
			<td width="181"><?php echo $row_rs_req['setor']; ?></td>        
			<td width="423"><?php echo $row_rs_req['descricao']; ?></td> 
		  </tr>      
		</table>
		</div>
</span></a>
  <?php } else { ?>
	  <a href="?index.php&pg=encaminhar&id=<?php echo $row_rs_req['id']; ?>&res" title="Clique para visualizar a Requisicao">
	  <div id="safe-result"><span style="color:#333;">
	   <table width="860" border="0">
		  <tr>
			<td width="62"><?php echo $row_rs_req['id']; ?></td>        
			<td width="90"><?php $data = $row_rs_req['data']; echo date("d/m/Y", strtotime($data));?></td>                
			<td width="83"><?php echo $row_rs_req['unidade']; ?></td>        
			<td width="181"><?php echo $row_rs_req['setor']; ?></td>        
			<td width="422"><?php echo $row_rs_req['descricao']; ?></td> 
		  </tr>      
		</table>
	  </div>
</span></a>
  <?php } } while ($row_rs_req = mysql_fetch_assoc($rs_req)); ?>
  
<?php
	mysql_free_result($rs_req);
?>